﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SampleApp {
    public partial class FormMain : Form {
        private string appVersion = "1.0.0";
        private string updaterPath = Path.Combine(Application.StartupPath, "Update.exe");
        
        public FormMain() {
            InitializeComponent();
            
            // 显示当前版本
            this.Text = $"示例应用 v{appVersion}";
            lblVersion.Text = $"当前版本: {appVersion}";
            
            // 检查更新器是否存在
            btnCheckUpdate.Enabled = File.Exists(updaterPath);
            
        }
        

        private void btnCheckUpdate_Click(object sender, EventArgs e) {
            try {
                // 启动更新器进程
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = updaterPath;
                startInfo.Arguments = $"{appVersion}";
                
                Process.Start(startInfo);
                
                // 关闭当前应用程序，让更新器接管
                Application.Exit();
            } catch (Exception ex) {
                MessageBox.Show("启动更新器失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void btnSilentUpdate_Click(object sender, EventArgs e) {
            try {
                // 启动更新器进程并传递自动关闭参数
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = updaterPath;
                startInfo.Arguments = $"{appVersion}";

                Process.Start(startInfo);
                
                // 关闭当前应用程序，让更新器接管
                Application.Exit();
            } catch (Exception ex) {
                MessageBox.Show("启动更新器失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FormMain_Load(object sender, EventArgs e) {

        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e) {

        }
    }
} 